<?php
  include_once('config/config.php');
  // 
  if($_POST){
	  // 判断电话号码是否存在
	  if($_POST['action'] == 'mobile'){
		  // 获取输入电话号码
		  $mobile = $_POST['val'];
		  // 查询本身
		  $person  = find("SELECT * FROM pre_person WHERE mobile = '$mobile';");
		  // 判断是否为空  为空可以使用
		  if($person ){
			  error('该手机号已被使用');
		  }else{
			  success('该手机号可以使用');
		  }
	  }
	  
	  // 判邮箱是否存在
	  if($_POST['action'] == 'email'){
	  		// 获取输入邮箱
	  		$email = $_POST['val'];
	  		// 查询本身
	  		$result = find("SELECT * FROM pre_person WHERE email = '$email';");
	  		// 判断是否为空  为空可以使用
	  		if($result){
	  			error('该邮箱已被使用');
	  		}else{
	  			success('该邮箱可以使用');
	  		}
	  }
	  
	  // 查询部门职位
	  if($_POST['action'] == 'depid'){
	  		// 获取id
	  		$depid = $_POST['depid'];
	  		// 查询本身
	  		$JobData  = all("SELECT * FROM pre_job WHERE depid = '$depid';");
	  		// 判断是否为空  为空可以使用
	  		if(!empty($JobData)){
	  			success('查询成功',$JobData);
	  		}else{
	  			error('查询失败');
	  		}
	  }
	 
	$avatar = upload('avatar');
	// 图片不可为空
	if(!isset($avatar['code']) && $avatar['code'] === 0)
	{
	  ShowMsg($avatar['msg']);
	  exit;
	}
	// 
	 $params = $_POST;
	// 封装数据
	$data = [
	  'name' => $params['name'],
	  'sex' => $params['sex'],
	  'mobile' => $params['mobile'],
	  'email' => $params['email'],
	  'address' => $params['address'],
	  'depid' => $params['depid'],
	  'jobid' => $params['jobid'],
	  'createtime' => time(),
	  'avatar' => $avatar['data']
	  ];
	// 
	$result = insert('person',$data);
	
	    if($result)
	    {
	      ShowMsg('新增成功','index.php');
	      exit;
	    }else{
	      ShowMsg('新增失败');
	      exit;
	    }
  }
  // 查询所有部门
  $DepData = all("SELECT * FROM pre_department;");
?>
<!DOCTYPE html>
<html lang="en">

<head>
  <!-- 引用公共样式 -->
  <?php include_once('common/meta.php'); ?>
</head>
<style>
  .avatar {
    width: 200px;
    user-select: none;
  }

  #img {
    width: 200px;
    display: block;
  }

  input[name="sex"]{
    margin-top: -2px;
  }
</style>

<body>

  <?php include_once('common/header.php') ?>

  <?php include_once('common/sidebar.php') ?>

  <div class="content">
    <div class="header">
      <h1 class="page-title">新增用户</h1>
    </div>
    <ul class="breadcrumb">
      <li><a href="index.php">Home</a> <span class="divider">/</span></li>
      <li class="active">AdminAdd</li>
    </ul>

    <div class="container-fluid">
      <div class="row-fluid">

        <div class="btn-toolbar">
          <button class="btn btn-primary" onClick="location='index.php'"><i class="icon-list"></i>用户列表</button>
          <div class="btn-group">
          </div>
        </div>

        <div class="well">
          <div id="myTabContent" class="tab-content">
            <div class="tab-pane active in" id="home">
              <form method="post" enctype="multipart/form-data">
                <!-- 用户名 -->
                <label>员工姓名</label>
                <input type="text" class="input-xxlarge" name="name" placeholder="请输入员工姓名" required>
                
                <!-- 手机号 -->
                <label>手机号</label>
                <input type="text" maxlength="11" oninput="value=value.replace(/[^\d]/g,'')" class="input-xxlarge" name="mobile" placeholder="请输入手机号" required>
                <span id="mobile"></span>

                <!-- 邮箱 -->
                <label>邮箱</label>
                <input type="text" class="input-xxlarge" name="email" placeholder="请输入邮箱" required>
				<span id="email"></span>

                <!-- 性别 -->
                <label>性别</label>
                <input type="radio" name="sex" id="Radio0" value="0" checked> <label style="display: inline-block;" for="Radio0">保密</label>
                <input type="radio" name="sex" id="Radio1" value="1"> <label style="display: inline-block;" for="Radio1">男</label>
                <input type="radio" name="sex" id="Radio2" value="2"> <label style="display: inline-block;" for="Radio2">女</label>

                <!-- 地址 -->
                <label>地址</label>
                <input type="text" class="input-xxlarge" name="address" placeholder="请输入地址" required>

                <!-- 所属部门 -->
                <label>所属部门</label>
                <select name="depid" id="depid">
                  <option value="0">请选择</option>
                  <?php foreach($DepData as $item){ ?>
				    <option value="<?php echo $item['id'] ?>"><?php echo $item['name'] ?></option>
				  <?php } ?>
                </select>

                <!-- 所属职位 -->
                <label>所属职位</label>
                <select name="jobid" id="jobid">
                  <option value="0">请选择</option>
                </select>

                <!-- 头像 -->
                <label>头像</label>
                <input type="file" name="avatar" id="avatar" hidden required>
                <label class="avatar" for="avatar">
                  <img src="static/images/upload.png" alt="" id="img">
                </label>

                <label></label>
                <input class="btn btn-primary" type="submit" value="提交" />
              </form>
            </div>
          </div>
        </div>

        <?php include_once('common/footer.php') ?>

      </div>
    </div>
  </div>
</body>

</html>
<?php include_once('common/script.php') ?>
<script>
  // 图片预览函数
  function GetObjectURL(file) {
    // createObjectURL  给一个文件对象 可以提取出一个url本地地址出来
    var url = null;

    //判断是否为IE浏览器还是google还是其他浏览器
    if (window.createObjectURL != undefined) {
      url = window.createObjectURL(file)
    } else if (window.URL != undefined) {
      url = window.URL.createObjectURL(file)
    } else if (window.webkitURL != undefined) {
      url = window.webkitURL.createObjectURL(file)
    }

    return url
  }

  $('#avatar').change(function(){

    // 获取选中的文件
    let file = $(this)[0].files[0] ? $(this)[0].files[0] : {};
    
    // 获取文件地址
    let url = GetObjectURL(file)

    // 赋值给img
    $('#img').attr('src',url)

  })

  $('#depid').change(function(){
    // 获取部门id
    let val = $(this).val()

    // 发起请求
    $.ajax({
      type:'post',
      url:'Personadd.php',
      data:{
        depid:val,
        action:'depid'
      },
      dataType:'json',
      success:function(result)
      {
        if(result.code === 1)
        {
          let option = ''

          result.data.forEach((item) => {
            option += `<option value="${item.id}">${item.name}</option>`
          });
          $('#jobid').html(option)
        }
      }
    })
  })

  $('input[name="mobile"]').change(function(){
    // 获取到电话号码的值
    let val = $(this).val()

    // 发起异步请求
    $.ajax({
      type:'post',
      url:'Personadd.php',
      data:{
        val,
        action:'mobile'
      },
      dataType:'json',
      success:function(result)
      {
        if(result.code === 1)
        {
          $('#mobile').html(result.msg).css({"color":"green"})
        }else{
          $('#mobile').html(result.msg).css({"color":"red"})
        }
      }
    })
  })
  
  $('input[name="email"]').change(function(){
    // 获取到电话号码的值
    let val = $(this).val()
  
    // 发起异步请求
    $.ajax({
      type:'post',
      url:'Personadd.php',
      data:{
        val,
        action:'email'
      },
      dataType:'json',
      success:function(result)
      {
        if(result.code === 1)
        {
          $('#email').html(result.msg).css({"color":"green"})
        }else{
          $('#email').html(result.msg).css({"color":"red"})
        }
      }
    })
  })

</script>